/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */
 struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
};
// @lc code=start


class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre = nullptr, *next = nullptr;
        while (head != nullptr) {
            next = head->next;
            head->next = pre; pre = head;
            head = next;
        }
        return pre;
    }

    // ListNode *reverseList(ListNode* head) {
    //     if (!head || !head->next) return head;
    //     ListNode* pre = nullptr, *current = head, *last = nullptr;
    //     while (current) {
    //         last = current->next;
    //         current->next = pre;
    //         pre = current;
    //         current = last;
    //     }
    //     return pre;
    // }
};
// @lc code=end

